package com.sc.core.make.file;

import com.baomidou.mybatisplus.extension.service.IService;
import com.sc.common.service.AbstractBaseService;
import com.sc.core.make.MasterMake;
import com.sc.core.make.build.*;
import com.sc.core.make.dto.MakeInfoDTO;
import com.sc.core.make.enums.ClassType;
import com.sc.core.util.DataAutoSearch.DataAutoSearch;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Primary;

import java.io.File;
import java.nio.file.Files;
import java.util.List;

@Log4j2
public class Service implements IFileMake{

    private final MakeInfoDTO makeInfo;

    @Getter
    private String message = "成功";

    public Service(MakeInfoDTO makeInfo) {
        this.makeInfo = makeInfo;
    }

    @Override
    public IFileMake make() {
        makeInterface(makeInfo.getClassname());
        makeImpl(makeInfo.getClassname());

        return this;
    }

    private void makeInterface(String classname) {
        String serviceClassname = "I" + classname + "Service";
        ClassBuild classBuild = new ClassBuild(serviceClassname, ClassType.INTERFACE);

        classBuild.setPackageName(makeInfo.getPackageName() + ".service");
        classBuild.setDocument(new String[]{makeInfo.getTableComment() + "服务接口"});
        classBuild.addImportClass(IService.class);
        classBuild.addImportClass(makeInfo.getPackageName() + "." + classname + "Entity");
        classBuild.setExtendClass(new GenericTypeBuild(IService.class).addGeneric(
                new GenericTypeBuild(classname + "Entity")
        ));

        classBuild.addMethod(getListMethodCreate(classname, classBuild));

        String filepath = MasterMake.getMakeDir(makeInfo.getModuleName()) + File.separator + "service" + File.separator + serviceClassname + ".java";

        String res = MasterMake.writeFile(filepath, classBuild.toString());
        if (!res.equals("成功")) {
            message = "ServiceInterface(失败)" + res;
        }
    }

    private MethodBuild getListMethodCreate(String classname, ClassBuild classBuild) {
        MethodBuild getList = new MethodBuild("getList");
        getList.setReturnType(new GenericTypeBuild(List.class).addGeneric(
                new GenericTypeBuild(classname + "Entity")
        ));
        getList.addMethodParam(new MethodParamBuild("dataAutoSearch", new GenericTypeBuild(DataAutoSearch.class)));
        classBuild.addImportClass(DataAutoSearch.class);
        classBuild.addImportClass(List.class);
        classBuild.addImportClass(makeInfo.getPackageName() + "." + makeInfo.getClassname() + "Entity");
        return getList;
    }

    private void makeImpl(String classname) {
        String serviceClassname = classname + "ServiceImpl";
        ClassBuild classBuild = new ClassBuild(serviceClassname);

        classBuild.setPackageName(makeInfo.getPackageName() + ".service.impl");
        classBuild.setDocument(new String[]{makeInfo.getTableComment() + "服务实现类"});
        classBuild.addImportClass(AbstractBaseService.class);
        classBuild.addImportClass(makeInfo.getPackageName() + ".mapper." + classname + "Mapper");
        classBuild.addImportClass(makeInfo.getPackageName() + "." + classname + "Entity");
        classBuild.addImportClass(makeInfo.getPackageName() + ".service.I" + classname + "Service");
        classBuild.addImportClass(Primary.class);
        classBuild.addImportClass(org.springframework.stereotype.Service.class);

        classBuild.setExtendClass(new GenericTypeBuild(AbstractBaseService.class).addGeneric(
                new GenericTypeBuild(classname + "Mapper"),
                new GenericTypeBuild(classname + "Entity")
        ));
        classBuild.addImplementClass("I" + classname + "Service");
        classBuild.addAnnotationClass(new AnnotationBuild(Primary.class));
        classBuild.addAnnotationClass(new AnnotationBuild(org.springframework.stereotype.Service.class));

        MethodBuild listMethod = getListMethodCreate(classname, classBuild);
        listMethod.addCode("return null;");
        classBuild.addMethod(listMethod);

        String filepath = MasterMake.getMakeDir(makeInfo.getModuleName()) + File.separator + "service" + File.separator + "impl" + File.separator + serviceClassname + ".java";

        String res = MasterMake.writeFile(filepath, classBuild.toString());
        if (!res.equals("成功")) {
            message = message.equals("成功")
                    ? "ServiceImpl(失败)" + res
                    : message + "ServiceImpl(失败)" + res;
        }
    }
}
